home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / commands.js < prev    next >
Text File  |  2008-05-17  |  9KB  |  414 lines

  1. /*
  2.     commands.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. var wot_commands =
  8. {
  9.     init: function()
  10.     {
  11.     },
  12.  
  13.     /* Determines the elements for which a tooltip should be shown */
  14.     tooltip_update: function(element)
  15.     {
  16.         try {
  17.             return (element == document.getElementById("wot-button") ||
  18.                     element == document.getElementById("wot-bar") ||
  19.                     element == document.getElementById("wot-bar-image"));
  20.         } catch (e) {
  21.             dump("wot_commands.tooltip_update: failed with " + e + "\n");
  22.         }
  23.         return false;
  24.     },
  25.  
  26.     update: function(what)
  27.     {
  28.         try {
  29.             if (!what) {
  30.                 what = "command";
  31.             }
  32.  
  33.             /* Enabled? */
  34.             document.getElementById("wot-" + what + "-enabled").
  35.                 setAttribute("checked", wot_prefs.enabled);
  36.  
  37.             var cached = wot_cache.isok(wot_core.hostname);
  38.  
  39.             /* Refresh */
  40.             document.getElementById("wot-" + what + "-refresh").
  41.                 setAttribute("disabled", !wot_prefs.enabled || !cached);
  42.  
  43.             /* Quick testimonies */
  44.             var quicks = new Array();
  45.  
  46.             for (var i = 0; i < 5; ++i) {
  47.                 quicks[i] =    document.getElementById("wot-" + what +
  48.                                 "-testify-" + (i + 1));
  49.             }
  50.  
  51.             for (var i = 0; i < quicks.length; ++i) {
  52.                 quicks[i].setAttribute("disabled",
  53.                     !wot_prefs.enabled || !cached);
  54.                 quicks[i].setAttribute("checked", false);
  55.             }
  56.  
  57.             if (wot_prefs.enabled && cached) {
  58.                 var t = wot_cache.get(wot_core.hostname, "testimony_0");
  59.  
  60.                 if (t >= WOT_MIN_REPUTATION_5) {
  61.                     quicks[4].setAttribute("checked", true);
  62.                 } else if (t >= WOT_MIN_REPUTATION_4) {
  63.                     quicks[3].setAttribute("checked", true);
  64.                 } else if (t >= WOT_MIN_REPUTATION_3) {
  65.                     quicks[2].setAttribute("checked", true);
  66.                 } else if (t >= WOT_MIN_REPUTATION_2) {
  67.                     quicks[1].setAttribute("checked", true);
  68.                 } else if (t >= 0) {
  69.                     quicks[0].setAttribute("checked", true);
  70.                 }
  71.             }
  72.         } catch (e) {
  73.             dump("wot_commands.update: failed with " + e + "\n");
  74.         }
  75.     },
  76.  
  77.     enabled: function()
  78.     {
  79.         try {
  80.             wot_prefs.enabled = !wot_prefs.enabled;
  81.             wot_prefs.setBool("enabled", wot_prefs.enabled);
  82.             wot_core.update();
  83.         } catch (e) {
  84.             dump("wot_commands.enabled: failed with " + e + "\n");
  85.         }
  86.     },
  87.  
  88.     refresh: function()
  89.     {
  90.         try {
  91.             if (wot_cache.iscached(wot_core.hostname)) {
  92.                 wot_cache.set(wot_core.hostname, "status",
  93.                     WOT_QUERY_RETRY);
  94.                 wot_core.update();
  95.             }
  96.         } catch (e) {
  97.             dump("wot_commands.refresh: failed with " + e + "\n");
  98.         }
  99.     },
  100.  
  101.     preferences: function()
  102.     {
  103.         try {
  104.             getBrowser().loadURI(WOT_PREF_URL);
  105.         } catch (e) {
  106.             dump("wot_commands.preferences: failed with " + e + "\n");
  107.         }
  108.     },
  109.  
  110.     my: function()
  111.     {
  112.         try {
  113.             getBrowser().loadURI(WOT_MY_URL);
  114.         } catch (e) {
  115.             dump("wot_commands.my: failed with " + e + "\n");
  116.         }
  117.     },
  118.  
  119.     quicktestify: function(value)
  120.     {
  121.         try {
  122.             if (wot_cache.isok(wot_core.hostname)) {
  123.                 wot_cache.set(wot_core.hostname, "testimony_0",
  124.                     Number(value));
  125.                 wot_cache.set(wot_core.hostname, "testimony_url",
  126.                     wot_browser.geturl());
  127.                 wot_cache.set(wot_core.hostname, "pending", true);
  128.                 wot_core.pending[wot_core.hostname] = true;
  129.                 wot_core.update();
  130.             }
  131.         } catch (e) {
  132.             dump("wot_commands.quicktestify: failed with " + e + "\n");
  133.         }
  134.     }
  135. };
  136.  
  137. wot_commands.init();
  138.  
  139. var wot_events =
  140. {
  141.     testimonydown: -1,
  142.  
  143.     init: function()
  144.     {
  145.     },
  146.  
  147.     get_slider_pos: function(event, testimony)
  148.     {
  149.         var pos = -1;
  150.         try {
  151.             var slider = document.getElementById("wot-rating-" +
  152.                 testimony + "-slider");
  153.             var sld_rule = wot_css.getstyle(WOT_STYLESHEET,
  154.                 ".wot-rating-slider");
  155.  
  156.             if (!slider || !sld_rule) {
  157.                 return pos;
  158.             }
  159.  
  160.             /* Calculate testimony value */
  161.             var sld_w = wot_css.getstyle_numeric(sld_rule, "width");
  162.  
  163.             pos = WOT_MAX_REPUTATION * (event.screenX -
  164.                         slider.boxObject.screenX) / sld_w;
  165.  
  166.             /* Limit to a valid range */
  167.             if (pos > WOT_MAX_REPUTATION) {
  168.                 pos = WOT_MAX_REPUTATION;
  169.             } else if (pos < 0) {
  170.                 pos = 0;
  171.             }
  172.  
  173.             /* Round */
  174.             pos = (pos / WOT_TESTIMONY_ROUND).toFixed() * WOT_TESTIMONY_ROUND;
  175.         } catch (e) {
  176.             dump("wot_events.get_slider_pos: failed with " + e + "\n");
  177.             pos = -1;
  178.         }
  179.         return pos;
  180.     },
  181.  
  182.     /* Handles testimony slider events and updates the new pending testimony to
  183.        query cache */
  184.     slider_down: function(event, testimony)
  185.     {
  186.         try {
  187.             if (!wot_cache.isok(wot_core.hostname)) {
  188.                 return false;
  189.             }
  190.  
  191.             this.testimonydown = testimony;
  192.  
  193.             var pos = this.get_slider_pos(event, testimony);
  194.             if (pos < 0) {
  195.                 return false;
  196.             }
  197.  
  198.             /* Insert into cache */
  199.             if (wot_cache.get(wot_core.hostname, "testimony_" +
  200.                     testimony) != pos) {
  201.                 wot_cache.set(wot_core.hostname, "testimony_" +
  202.                     testimony, Number(pos));
  203.                 wot_cache.set(wot_core.hostname, "testimony_url",
  204.                     wot_browser.geturl());
  205.                 wot_cache.set(wot_core.hostname, "pending", true);
  206.                 wot_core.pending[wot_core.hostname] = true;
  207.  
  208.                 /* Update testimony window */
  209.                 wot_ui.update_testimonies();
  210.             }
  211.  
  212.             /* Any pending testimonies will be stored in wot_core.update,
  213.                 which is called when the popup window is closed */
  214.             return true;
  215.         } catch (e) {
  216.             dump("wot_events.slider: failed with " + e + "\n");
  217.         }
  218.         return false;
  219.     },
  220.  
  221.     slider_up: function(event, testimony)
  222.     {
  223.         this.testimonydown = -1;
  224.         return true;
  225.     },
  226.  
  227.     slider_move: function(event, testimony)
  228.     {
  229.         /* Apparently, there is no way to detect if the mouse button is
  230.             down besides counting clicks. This means that if the mouse
  231.             button is released while outside the window, we won't be able
  232.             to detect it and the slider keeps moving... */
  233.         if (this.testimonydown == testimony) {
  234.             this.slider_down(event, testimony);
  235.         } else {
  236.             this.testimonydown = -1;
  237.             if (wot_cache.isok(wot_core.hostname)) {
  238.                 var pos = this.get_slider_pos(event, testimony);
  239.                 if (pos >= 0) {
  240.                     wot_ui.update_testimonies(testimony, pos);
  241.                 }
  242.             }
  243.         }
  244.         return true;
  245.     },
  246.  
  247.     /* Hides the popup window */
  248.     popup_hide: function()
  249.     {
  250.         try {
  251.             var popup = document.getElementById("wot-popup");
  252.             if (popup) {
  253.                 popup.hidePopup();
  254.             }
  255.         } catch (e) {
  256.             dump("wot_events.popup_hide: failed with " + e + "\n");
  257.         }
  258.         return false;
  259.     },
  260.  
  261.     /* Called when the popup window is hidden */
  262.     hide_popup: function()
  263.     {
  264.         try {
  265.             if (wot_api_query.message_id.length > 0 &&
  266.                 wot_api_query.message_id !=
  267.                     WOT_SERVICE_XML_QUERY_MSG_ID_MAINT) {
  268.                 wot_prefs.setChar("last_message", wot_api_query.message_id);
  269.             }
  270.  
  271.             /* Stores any pending testimonies */
  272.             wot_core.update();
  273.         } catch (e) {
  274.             dump("wot_events.hide_popup: failed with " + e + "\n");
  275.         }
  276.     },
  277.  
  278.     click_title: function(event) {
  279.         try {
  280.             if (!wot_prefs.enabled) {
  281.                 wot_commands.enabled();
  282.             }
  283.         } catch (e) {
  284.             dump("wot_events.click_title: failed with " + e + "\n");
  285.         }
  286.     },
  287.  
  288.     click_help: function(event, i) {
  289.         try {
  290.             var link = document.getElementById("wot-rating-" + i + "-help-link");
  291.             if (link && link.getAttribute("comment") == "true") {
  292.                 return this.click_scorecard(event, true);
  293.             }
  294.         } catch (e) {
  295.             dump("wot_events.click_help: failed with " + e + "\n");
  296.         }
  297.         return false;
  298.     },
  299.  
  300.     click_user: function(event, i)
  301.     {
  302.         try {
  303.             var content = document.getElementById("wot-user-" + i + "-content");
  304.  
  305.             if (!content) {
  306.                 return false;
  307.             }
  308.  
  309.             var browser = getBrowser();
  310.             if (browser) {
  311.                 browser.selectedTab =
  312.                     browser.addTab(content.getAttribute("url"));
  313.                 this.popup_hide();
  314.             }
  315.         } catch (e) {
  316.             dump("wot_events.click_user: failed with " + e + "\n");
  317.         }
  318.         return false;
  319.     },
  320.  
  321.     click_scorecard: function(event, comment)
  322.     {
  323.         try {
  324.             if (wot_core.hostname) {
  325.                 var browser = getBrowser();
  326.                 if (browser) {
  327.                     var url = WOT_SCORECARD_URL +
  328.                         encodeURIComponent(wot_core.hostname);
  329.                     if (comment) {
  330.                         url += WOT_SCORECARD_COMMENT;
  331.                     }
  332.                     browser.selectedTab = browser.addTab(url);
  333.                     this.popup_hide();
  334.                 }
  335.             }
  336.         } catch (e) {
  337.             dump("wot_events.click_scorecard: failed with " + e + "\n");
  338.         }
  339.         return false;
  340.     },
  341.  
  342.     click_logo: function(event)
  343.     {
  344.         try {
  345.             var browser = getBrowser();
  346.             if (browser) {
  347.                 browser.selectedTab = browser.addTab(WOT_MY_URL);
  348.                 this.popup_hide();
  349.             }
  350.         } catch (e) {
  351.             dump("wot_events.click_guide: failed with " + e + "\n");
  352.         }
  353.         return false;
  354.     },
  355.  
  356.     click_guide: function(event)
  357.     {
  358.         try {
  359.             var browser = getBrowser();
  360.             if (browser) {
  361.                 browser.selectedTab = browser.addTab(WOT_GUIDE_URL);
  362.                 this.popup_hide();
  363.             }
  364.         } catch (e) {
  365.             dump("wot_events.click_guide: failed with " + e + "\n");
  366.         }
  367.         return false;
  368.     },
  369.  
  370.     click_prefs: function(event)
  371.     {
  372.         try {
  373.             var browser = getBrowser();
  374.             if (browser) {
  375.                 browser.selectedTab = browser.addTab(WOT_PREF_URL);
  376.                 this.popup_hide();
  377.             }
  378.         } catch (e) {
  379.             dump("wot_events.click_prefs: failed with " + e + "\n");
  380.         }
  381.         return false;
  382.     },
  383.  
  384.     click_update: function(event)
  385.     {
  386.         try {
  387.             this.popup_hide();
  388.             wot_update.update(true);
  389.         } catch (e) {
  390.             dump("wot_events.click_update: failed with " + e + "\n");
  391.         }
  392.         return false;
  393.     },
  394.  
  395.     click_message: function(event)
  396.     {
  397.         try {
  398.             if (wot_url.issupported(wot_api_query.message_url)) {
  399.                 var browser = getBrowser();
  400.                 if (browser) {
  401.                     browser.selectedTab =
  402.                         browser.addTab(wot_api_query.message_url);
  403.                     this.popup_hide();
  404.                 }
  405.             }
  406.         } catch (e) {
  407.             dump("wot_events.click_message: failed with " + e + "\n");
  408.         }
  409.         return false;
  410.     }
  411. };
  412.  
  413. wot_events.init();
  414.